package cn.com.pet.administrator.action.system;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import cn.com.pet.common.base.BaseAction;
import cn.com.pet.common.base.ErrorMessage;
import cn.com.pet.common.entity.AdminUser;
import cn.com.pet.common.entity.Privilege;
import cn.com.pet.common.service.PrivilegeService;
import cn.com.pet.common.util.StringUtil;

@Namespace("/action")
@ParentPackage("base")
@SuppressWarnings({ "serial" })
public class PrivilegeAction extends BaseAction {

	@Autowired
	PrivilegeService privilegeService;

	@Action(value = "adminUserList", results = {
			@Result(name = SUCCESS, location = "/html/administrator/system/privilegeList.jsp") })
	public String getAdminUserList() {

		long privilegeId = StringUtil.parsLong(this.getRequest().getParameter("id"));
		String privilegeName = this.getRequest().getParameter("name");
		long privilegeParent = StringUtil.parsLong(this.getRequest().getParameter("pid"));
		int privilegeType = StringUtil.parsInt(this.getRequest().getParameter("type"));

		Privilege privilege = new Privilege();
		privilege.setPrivilegeId(privilegeId);
		privilege.setPrivilegeName(privilegeName);
		privilege.setPrivilegeParent(privilegeParent);
		privilege.setPrivilegeType(privilegeType);

		List<AdminUser> result = privilegeService.getPrivilegeList(privilege);

		this.getRequest().setAttribute("adminUserList", result);

		return SUCCESS;
	}

	@Action(value = "pageUpdateAdminUser", results = {
			@Result(name = SUCCESS, location = "/html/administrator/system/updatePrivilege.jsp") })
	public String pageUpdateAdminUser() {

		long id = StringUtil.parsLong(this.getRequest().getParameter("id"));

		Privilege privilege = new Privilege();
		privilege.setPrivilegeId(id);

		AdminUser result = privilegeService.getPrivilegeById(privilege);

		this.getRequest().setAttribute("adminUser", result);

		return SUCCESS;
	}

	@Action(value = "adminUserUpdate", results = {
			@Result(name = SUCCESS, location = "/html/administrator/system/privilegeList.jsp") })
	public String adminUserUpdate() {
		
		long privilegeId = StringUtil.parsLong(this.getRequest().getParameter("id"));
		String privilegeName = this.getRequest().getParameter("name");
		long privilegeParent = StringUtil.parsLong(this.getRequest().getParameter("pid"));
		int privilegeOrder = StringUtil.parsInt(this.getRequest().getParameter("order"));
		String privilegeUrl = this.getRequest().getParameter("url");
		int privilegeType = StringUtil.parsInt(this.getRequest().getParameter("type"));
		String remark = this.getRequest().getParameter("remark");

		Privilege privilege = new Privilege();
		privilege.setPrivilegeId(privilegeId);
		privilege.setPrivilegeName(privilegeName);
		privilege.setPrivilegeParent(privilegeParent);
		privilege.setPrivilegeOrder(privilegeOrder);
		privilege.setPrivilegeUrl(privilegeUrl);
		privilege.setPrivilegeType(privilegeType);
		privilege.setStatus(1);
		privilege.setRemark(remark);

		int result = privilegeService.updatePrivilege(privilege);
		if (result > 0) {
			return SUCCESS;
		} else {
			this.getRequest().setAttribute("error", ErrorMessage.PRIVIL_UPDATE_ERROR);
			return ERROR;
		}
	}

	@Action(value = "pageAddAdminUser", results = {
			@Result(name = SUCCESS, location = "/html/administrator/system/addPrivilege.jsp") })
	public String pageAddAdminUser() {
		return SUCCESS;
	}

	@Action(value = "addAdminUser", results = {
			@Result(name = SUCCESS, location = "/html/administrator/system/privilegeList.jsp") })
	public String addAdminUser() {

		long privilegeId = StringUtil.parsLong(this.getRequest().getParameter("id"));
		String privilegeName = this.getRequest().getParameter("name");
		long privilegeParent = StringUtil.parsLong(this.getRequest().getParameter("pid"));
		int privilegeOrder = StringUtil.parsInt(this.getRequest().getParameter("order"));
		String privilegeUrl = this.getRequest().getParameter("url");
		int privilegeType = StringUtil.parsInt(this.getRequest().getParameter("type"));
		String remark = this.getRequest().getParameter("remark");

		Privilege privilege = new Privilege();
		privilege.setPrivilegeId(privilegeId);
		privilege.setPrivilegeName(privilegeName);
		privilege.setPrivilegeParent(privilegeParent);
		privilege.setPrivilegeOrder(privilegeOrder);
		privilege.setPrivilegeUrl(privilegeUrl);
		privilege.setPrivilegeType(privilegeType);
		privilege.setStatus(1);
		privilege.setRemark(remark);

		int result = privilegeService.addPrivilege(privilege);
		if (result > 0) {
			return SUCCESS;
		} else {
			this.getRequest().setAttribute("error", ErrorMessage.PRIVIL_ADD_ERROR);
			return ERROR;
		}

	}

}
